﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Top.Api.Domain;
using System.Data.Objects;
using Top.Api.Response;
using Top.Api;
using Top.Api.Request;
using System.Configuration;
namespace TopModel
{
    public class TopTradeHelper
    {
        /// <summary>
        /// 将数据导入数据库
        /// </summary>
        /// <param name="trade"></param>
        /// <param name="rate"></param>
        public void AddTopTrade(Trade trade, TradeRate rate)
        {
            try
            {
                using (a1009210041Entities entity = new a1009210041Entities())
                {
                    TopTrade tTrade = new TopTrade();
                    tTrade.Tid = trade.Tid.ToString();
                    tTrade.BuyerNick = trade.BuyerNick;
                    tTrade.SellerNick = trade.SellerNick;
                    tTrade.ReceiverState = trade.ReceiverState;
                    tTrade.RecevierCity = trade.ReceiverCity;
                    tTrade.ReceiverPhone = trade.ReceiverPhone != null ? trade.ReceiverPhone : string.Empty;
                    tTrade.ReceiverMobile = trade.ReceiverMobile;
                    tTrade.ReceiverName = trade.ReceiverName;
                    tTrade.ReceiverDistrict = trade.ReceiverDistrict;
                    tTrade.ReceiverAddress = trade.ReceiverAddress != null ? trade.ReceiverAddress : string.Empty;
                    tTrade.Oid = rate.Oid.ToString();
                    tTrade.Title = rate.ItemTitle;
                    tTrade.RateType = Convert.ToInt16((ERateType)Enum.Parse(typeof(ERateType), rate.Result));
                    entity.AddToTopTrade(tTrade);
                    entity.SaveChanges();
                }
            }
            catch (Exception e)
            {
                throw new Exception("导入淘宝数据错误", e);
            }
        }

        /// <summary>
        /// 获取拼接的数据并导入
        /// </summary>
        /// <param name="sessionKey"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        public int ImportData(string sessionKey, DateTime startDate, DateTime endDate)
        {
            TopTradeHelper tradeHelper = new TopModel.TopTradeHelper();
            TraderatesGetResponse rspRate = new TraderatesGetResponse();
            TradeFullinfoGetResponse rspTrade = new TradeFullinfoGetResponse();
            short status = 1; //正常
            try
            {
                ITopClient topClient = new DefaultTopClient(ConfigurationManager.AppSettings["TopInterfaceUrl"], ConfigurationManager.AppSettings["TopAppKey"], ConfigurationManager.AppSettings["TopAppSecret"], "xml");
                TraderatesGetRequest reqRate = new TraderatesGetRequest();
                TradeFullinfoGetRequest reqTrade = new TradeFullinfoGetRequest();
                reqRate.Fields = "tid,item_title,content,result,oid";
                reqRate.RateType = "give";
                //req.Result = type.ToString();
                reqRate.Role = "buyer";
                reqRate.StartDate = startDate;
                reqRate.EndDate = endDate;
                rspRate = topClient.Execute(reqRate, sessionKey);
                reqTrade.Fields = "seller_nick,buyer_nick,receiver_name,receiver_state,receiver_city,receiver_district,receiver_mobile,receiver_phone";
                foreach (TradeRate rate in rspRate.TradeRates)
                {
                    reqTrade.Tid = rate.Tid;
                    rspTrade = topClient.Execute(reqTrade, sessionKey);
                    tradeHelper.AddTopTrade(rspTrade.Trade, rate);
                }
            }
            catch (Exception e)
            {
                status = 0;
            }
            return status;
        }

        /// <summary>
        /// 获取最后的导入日期
        /// </summary>
        /// <param name="nick"></param>
        /// <returns></returns>
        public DateTime GetLastImportTime(string nick)
        {
            try
            {
                ImportTime time = new ImportTime();
                using (a1009210041Entities entity = new a1009210041Entities())
                {
                    time = entity.GetLastImportTime(nick).FirstOrDefault();
                }
                return time.CreateTime;
            }
            catch (Exception e)
            {
                throw new Exception("获取最后的导入日期失败", e);
            }
        }

    }
}

